package com.toket.android.pushme.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.toket.android.pushme.R;
import com.toket.android.pushme.info.ContentInfo;
import com.toket.android.pushme.parser.JSONParser;
import com.toket.android.pushme.screen.MainScreen;
import com.toket.android.utils.ApplicationConfigs;

public class ContentView extends AbstractView{
	
	private LinearLayout Layout_content_view;
	private ListView listview_content;
	private MyViewAdapter myAdb;
	
	private TextView text_date;
	private TextView text_title_room, text_title_device, text_title_usetime;
	
	public ContentView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public void setVisibleView(int viewmode){
		Layout_content_view.setVisibility(viewmode);
	}

	@Override
	protected void initLayout() {
		// TODO Auto-generated method stub
		Layout_content_view = (LinearLayout) screen.findViewById(R.id.Layout_content_view);
		listview_content = (ListView) screen.findViewById(R.id.listview_content);
		
		text_date = (TextView) screen.findViewById(R.id.text_date);
		
		text_title_room		= (TextView) screen.findViewById(R.id.text_title_room);
		text_title_device	= (TextView) screen.findViewById(R.id.text_title_device);
		text_title_usetime	= (TextView) screen.findViewById(R.id.text_title_usetime);
	}
	
	private int viewWidth_room, viewWidth_device, viewWidth_usetime;
	@SuppressLint("SimpleDateFormat")
	@Override
	protected void initValue() {
		// TODO Auto-generated method stub
		
		text_date.setVisibility(View.INVISIBLE);
//		setCurrentDate();
		
//		int viewWidth = screenWidth/4;
		
		viewWidth_room = 95;
		viewWidth_usetime = 135;
		viewWidth_device = screenWidth - viewWidth_room - viewWidth_usetime;
		
		LayoutParams params_room = new LinearLayout.LayoutParams(
				new LayoutParams(viewWidth_room, LayoutParams.WRAP_CONTENT));
		LayoutParams params_device = new LinearLayout.LayoutParams(
				new LayoutParams(viewWidth_device, LayoutParams.WRAP_CONTENT));
		LayoutParams params_usetime = new LinearLayout.LayoutParams(
				new LayoutParams(viewWidth_usetime, LayoutParams.WRAP_CONTENT));
		
//		text_title_room.setBackgroundColor(Color.BLUE);
//		text_title_usetime.setBackgroundColor(Color.CYAN);
		
		text_title_room.setLayoutParams(params_room);
		text_title_device.setLayoutParams(params_device);
		text_title_usetime.setLayoutParams(params_usetime);
		
		ArrayList<ContentInfo> items = new ArrayList<ContentInfo>();
		
		myAdb = new MyViewAdapter(context, R.layout.item_contentview, items);
		listview_content.setAdapter(myAdb);
		
		startParser(ApplicationConfigs.URL_CONTENT);
		
	}
	
	@SuppressLint("SimpleDateFormat")
	private void setCurrentDate() {
		Calendar c = Calendar.getInstance();

		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		String formattedDate = df.format(c.getTime());
		
		text_date.setText(formattedDate);
		text_date.setVisibility(View.VISIBLE);
	}

	public void refreshLoader(){
		startParser(ApplicationConfigs.URL_CONTENT);
	}

	private FeedContent feeder;
	private void startParser(String url) {
		((MainScreen) context).showProgressLoader(true);
		
		if(feeder != null){
			feeder.cancel(true);
		}
		feeder = new FeedContent(url);
		feeder.execute("");
	}
	
	private class FeedContent extends AsyncTask<String, Void, ArrayList<ContentInfo>>{
		
		private String url_;
		
		public FeedContent(String url_){
			this.url_ = url_;
		}
		
		@Override
		protected ArrayList<ContentInfo> doInBackground(String... params) {
			// TODO Auto-generated method stub
			ArrayList<ContentInfo> items = null;
			JSONParser parser = new JSONParser(url_);
			if(parser.connectInputStream()){
				items = parser.feedContentInfo();
			}
			return items;
		}

		@Override
		protected void onPostExecute(ArrayList<ContentInfo> items) {
			// TODO Auto-generated method stub
			((MainScreen) context).showProgressLoader(false);
			setCurrentDate();
			if(items != null){
				myAdb.setItems(items);
				myAdb.notifyDataSetChanged();
				if(items.size() == 0){
					Toast.makeText(context, screen.getResources().getString(R.string.no_content), 
							Toast.LENGTH_LONG).show();
				}
			}else{
				Toast.makeText(context, screen.getResources().getString(R.string.connection_fail), 
						Toast.LENGTH_LONG).show();
			}
			
		}
		
	}
	
	private class MyViewAdapter extends ArrayAdapter<ContentInfo>{
		
		private Context mContext;
		private int layout;
		private ArrayList<ContentInfo> items;
		
		public MyViewAdapter(Context context, int layoutResource,
				ArrayList<ContentInfo> items){
			super(context, layoutResource, items);
			
			this.mContext = context;
			this.layout = layoutResource;
			this.items = items;
			
		}
		
		public void setItems(ArrayList<ContentInfo> items){
			this.items.clear();
			this.items.addAll(items);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return items.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v = convertView;
			
	        if (v == null) {
	           LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	           v = vi.inflate(layout, null);
	        }
	        
	        TextView text_item_room		= (TextView) v.findViewById(R.id.text_item_room);
	        TextView text_item_device	= (TextView) v.findViewById(R.id.text_item_device);
	        TextView text_item_usetime	= (TextView) v.findViewById(R.id.text_item_usetime);
	        
//	        int viewWidth = screenWidth/4;
			
			LayoutParams params_room = new LinearLayout.LayoutParams(
					new LayoutParams(viewWidth_room, LayoutParams.WRAP_CONTENT));
			LayoutParams params_device = new LinearLayout.LayoutParams(
					new LayoutParams(viewWidth_device, LayoutParams.WRAP_CONTENT));
			LayoutParams params_usetime = new LinearLayout.LayoutParams(
					new LayoutParams(viewWidth_usetime, LayoutParams.WRAP_CONTENT));
			
			text_item_room.setLayoutParams(params_room);
			text_item_device.setLayoutParams(params_device);
			text_item_usetime.setLayoutParams(params_usetime);
			
			text_item_room.setSelected(true);
			text_item_device.setSelected(true);
			text_item_usetime.setSelected(true);
			
	        if(items != null){
		        text_item_room.setText((items.get(position).getRoom_id() == null) ? 
		        		"" : items.get(position).getRoom_id());
		        text_item_device.setText((items.get(position).getEquipment() == null) ? 
		        		"" : items.get(position).getEquipment());
		        text_item_usetime.setText((items.get(position).getPeriod_start() == null) ? 
		        		"" : items.get(position).getPeriod_start());
	        }
	        
			return v;
		}
		
	}

	@Override
	public void cleanUp() {
		// TODO Auto-generated method stub
		
	}

}
